<?php

namespace app\admin\controller;

use app\common\model\Member as MemberModel;
use app\common\model\MemberQuality as MemberQualityModel;
use app\common\model\Config as ConfigModel;
use think\Db;
/**
 * 用户修为控制器
 * Class Memberquality
 * @package app\index\controller
 */
class Memberquality extends Base
{
    /**
     * [getqualityleavel  获取修为等级]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getqualityleavel()
    {
        $model = new ConfigModel();
        try{
            $quality_leavel = $model->findOne(['cname'=>'quality_leavel'],'cvalue');
            if(empty($quality_leavel['cvalue'])){
                $leavel = config('quality.quality_leavel');
            }else{
                $leavel = unserialize($quality_leavel['cvalue']);
            }
        } catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'获取成功',$leavel);
    }

    /**
     * [savequalityleavel  保存修为等级]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function savequalityleavel()
    {
        $param = input('param.');
        $model = new ConfigModel();
        try{
            $model->SaveConfig(['cname'=>'quality_leavel'],serialize($param['quality_leavel']));
        } catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'保存成功');
    }

    /**
     * [getqualityconfig  获取修为配置]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getqualityconfig()
    {
        $model = new ConfigModel();
        $data = [];
        $user = $this->user;
        try {
            $signin = $model->findOne(['cname'=>'daily_signin'],'cvalue');
            if (empty($signin['cvalue'])) {
                $data['daily_signin'] = config('quality.daily_signin');
            } else {
                $data['daily_signin'] = unserialize($signin['cvalue']);
            }
            $help = $model->findOne(['cname' => 'help_people'], 'cvalue');
            if (empty($help['cvalue'])) {
                $data['help_people'] = config('quality.help_people');
            } else {
                $data['help_people'] = unserialize($help['cvalue']);
            }
            $word = $model->findOne(['cname' => 'work_hard'], 'cvalue');
            if (empty($word['cvalue'])) {
                $data['work_hard'] = config('quality.work_hard');
            } else {
                $data['work_hard'] = unserialize($word['cvalue']);
            }
            $temple_interaction = $model->findOne(['cname' => 'temple_interaction'], 'cvalue');
            if (empty($temple_interaction['cvalue'])) {
                $data['temple_interaction'] = config('quality.temple_interaction');
            } else {
                $data['temple_interaction'] = unserialize($temple_interaction['cvalue']);
            }
            $glance_content = $model->findOne(['cname' => 'glance_content'], 'cvalue');
            if (empty($glance_content['cvalue'])) {
                $data['glance_content'] = config('quality.glance_content');
            } else {
                $data['glance_content'] = unserialize($glance_content['cvalue']);
            }
            $account_information = $model->findOne(['cname' => 'account_information'], 'cvalue');
            if (empty($account_information['cvalue'])) {
                $data['account_information'] = config('quality.account_information');
            } else {
                $data['account_information'] = unserialize($account_information['cvalue']);
            }
            $is_continuous = $model->findOne(['cname' => 'is_continuous'], 'cvalue');
            if(empty($is_continuous['is_continuous'])){
                $data['is_continuous'] = config('quality.is_continuous');
            }else{
                $data['is_continuous'] = $is_continuous['cvalue'];
            }
        } catch (\Exception $e) {
            return admin_json(-1, $e->getMessage());
        }
        writelog('查看修为配置',1,$user);
        return admin_json(1, '获取成功', $data);
    }

    /**
     * [savequalityconfig  保存修为配置]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function savequalityconfig()
    {
        $param = input('param.');
        $model = new ConfigModel();
        $user = $this->user;
        try{
            $model->SaveConfig(['cname'=>'daily_signin'],serialize($param['daily_signin']));
            $model->SaveConfig(['cname'=>'help_people'],serialize($param['help_people']));
            $model->SaveConfig(['cname'=>'work_hard'],serialize($param['work_hard']));
            $model->SaveConfig(['cname'=>'temple_interaction'],serialize($param['temple_interaction']));
            $model->SaveConfig(['cname'=>'glance_content'],serialize($param['glance_content']));
            $model->SaveConfig(['cname'=>'account_information'],serialize($param['account_information']));
            $model->SaveConfig(['cname'=>'is_continuous'],$param['is_continuous']);
        } catch (\Exception $e) {
            return admin_json(-1, $e->getMessage());
        }
        writelog('保存修为配置',1,$user);
        return admin_json(1, '保存成功');
    }

    /**
     * [qualitylist  修为榜单]
     * @param type 1总榜单 2月榜单 3周榜单 4日榜单
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function qualitylist()
    {
        $param = input('param.');
        $model = new MemberModel;
        $cmodel = new ConfigModel;
        $mqmodel = new MemberQualityModel;
        $user = $this->user;
        try{
            if($param['type'] == 1){
                $map = '';
            }elseif($param['type'] == 2){
                $year = date('Y');
                $mon = date('n');
                $start = strtotime($year.'-'.$mon.'-1');
                $mon1 = $mon + 1;
                if($mon1 == 13){
                    $mon1 = 1;
                    $year1 = $year+1;
                    $end = strtotime($year1.'-'.$mon1.'-1');
                }else{
                    $end = strtotime($year.'-'.$mon1.'-1');
                }

                $map = 'where create_time between ' . $start . ' and ' . $end;
            }elseif($param['type'] == 3){
                $w = date('w');
                if($w == 0){
                    $w = 7;
                }
                $start = strtotime(date('Y-m-d')) - ($w - 1)*24*3600;
                $end = $start + 7*24*3600;
                $map = 'where create_time between ' . $start . ' and ' . $end;
            }elseif($param['type'] == 4){
                $start = strtotime(date('Y-m-d'));
                $end = strtotime(date('Y-m-d')) + 24*3600;
                $map = 'where create_time between ' . $start . ' and ' . $end;
            }
            if (!empty($param['start']) && !empty($param['end'])) {
                $map = 'where create_time between ' . strtotime($param['start']) . ' and ' . strtotime($param['end']);
            }
            $sql = 'SELECT mid, sum(quality) AS count  FROM app_member_quality '.$map.' GROUP BY mid  ORDER BY count DESC,mid DESC LIMIT 100';
            $list = Db::query($sql);
            foreach($list as &$v){
                $memberquality = $mqmodel->findOne(['mid'=>$v['mid'],'type'=>40],'id,mid,quality,type,create_time,day','id','desc');
                $member = $model->findData($v['mid'],'id','username,tel,img,quality');
                $http = substr($member['img'],0,4);
                if($http != 'http'){
                    $member['img'] = config('domain_name').$member['img'];
                }
                $v['member'] = $member;
                $v['member_quality'] = $memberquality;
                $quality_leavel = $cmodel->findData('quality_leavel','cname','cvalue');
                if(empty($quality_leavel['cvalue'])){
                    $quality_leavel = config('quality.quality_leavel');
                }else{
                    $quality_leavel = unserialize($quality_leavel['cvalue']);
                }
                foreach($quality_leavel as $k=>$v1){
                    if($member['quality'] >= $v1['quality'] && $member['quality'] < $quality_leavel[$k+1]['quality']){
                        $v['level'] = $v1['name'];
                        $v['level_num'] = $v1['level'];
                    }
                }
            }
        } catch (\Exception $e) {
            return admin_json(-1, $e->getMessage());
        }
        writelog('查看修为榜单',1,$user);
        return admin_json(1, '获取成功',$list);
    }
}